let blockId = '';
chrome.webRequest.onBeforeRequest.addListener(
    (details) => {
        let body = new TextDecoder("utf-8").decode(details.requestBody.raw[0].bytes);
        let jsonBody = JSON.parse(body);
        blockId = jsonBody?.selected_blocks?.[0]?.block_id;
    },
    { urls: ['https://thinkingdata.feishu.cn/space/api/docx/partial_content_anchor/create'] },
    ["requestBody"]
);
chrome.webRequest.onCompleted.addListener(
    (details) => {
        chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
            if (tabs.length > 0 && blockId) {
              chrome.scripting.executeScript({
                target: { tabId: tabs[0].id },
                function: (text) => {
                    setTimeout(() => {
                        navigator.clipboard.writeText(`${location.origin}${location.pathname}#${text}`); 
                    }, 200);
                },
                args: [blockId]
              });
            }
          });
    },
    { urls: ['https://thinkingdata.feishu.cn/space/api/docx/partial_content_anchor/create'] },
);